package com.platform.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.base.R;
import com.platform.base.mvc.BaseController;
import com.platform.dto.RoleRight;
import com.platform.exception.BizException;
import com.platform.logger.annotation.ApiLog;
import com.platform.service.IRoleRight;
import com.platform.utils.StringUtils;

import cn.hutool.core.map.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 
 * <p> Title:Jsqx Mapper</p>
 * <p> desc:  角色权限表</p>
 * <p> Copyright: Copyright (c) 2019 </p>
 * <p> Company: </p>
 *
 * @author wys
 * @version 1.0
 */
@Controller
@RequestMapping("/roleRight") 
@Api(tags="OMS-角色授权模块")
public class RoleRightController extends BaseController{
	
	@Autowired
    private IRoleRight services;
    
    @RequestMapping(value="data",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="获取所有数据")
    public Object selectAll(HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
        Map<String,Object> map = MapUtil.newHashMap();
        List<RoleRight> servicesList = services.selectAll(map);
        return R.success(servicesList);
    }
    
    @ApiLog(value="删除角色权限绑定关系数据",desc="系统角色/权限数据删除")
    @RequestMapping(value="delete",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="删除数据")
    public Object delete(String ids,HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
    	boolean isSuccess = false;
    	String[] codes = ids.split(",");
    	for(String pk : codes){
    		RoleRight dto = services.get(pk);
    		isSuccess = services.delete(dto);
    	}
    	if(isSuccess){
    		return R.success();
    	}else {
    		return R.fail();
    	}
    }
    
    @ApiLog(value="新增角色权限数据绑定",desc="系统角色/权限数据保存")
    @RequestMapping(value="save",method=RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="保存数据")
    public Object insert(@Valid RoleRight dto,HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
        dto.setCreateDate(new Date());
        dto.setCreater(getName());
        if(services.insert(dto)) {
        	return R.success();
    	}else {
    		return R.fail();
    	}
    }
    
    @ApiLog(value="角色/权限数据关系绑定",desc="系统角色/权限数据关系绑定")
    @RequestMapping(value="rightRoleAuthorization",method= {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value="角色、权限关系绑定")
    public Object rightRoleAuthorization(String r_code,String ri_code,HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
    	try {
			services.deleteByJsId(Long.valueOf(r_code));
			List<String> rightCodeList = StringUtils.StringToList(ri_code, ",");
			List<RoleRight> roleRightList = new ArrayList<RoleRight>();
			for(String str : rightCodeList){
				RoleRight r = new RoleRight();
				r.setRoleid(Long.valueOf(r_code));
				r.setRightid(Long.valueOf(str));
				r.setCreateDate(new Date(System.currentTimeMillis()));
				r.setCreater(getName());
				roleRightList.add(r);
//				services.insert(r);
			}
			services.insertBatch(roleRightList);
			return R.success();
		} catch (NumberFormatException e) {
			return R.fail(e);
		} catch (BizException e) {
			return R.fail(e);
		}
    }
    
    @RequestMapping(value="edit",method=RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改数据")
    public Object update(@Valid RoleRight dto, HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
    	dto.setLastModifier(getName());
    	dto.setLastModifyDate(new Date(System.currentTimeMillis()));
        return services.update(dto);
    }
    
    @RequestMapping(value="find",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="获取单条数据")
    public Object get(String code, HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
        return services.get(code);
    }
	
}
